package com.sky.controller.admin;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    DishService dishService;
    @PostMapping
    @ApiOperation("新增菜品")
    @CachePut(value = "dish", key = "#dish.id")
    public Result saveWithflavor(@RequestBody DishDTO dish) {
        dishService.saveWithflavor(dish);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO query) {
        PageResult pageResult=dishService.page(query);
        return Result.success(pageResult);
    }
    @ApiOperation("删除菜品")
    @DeleteMapping()
    @CacheEvict(value = "dish", allEntries = true)
    public Result delete(String ids) {
        dishService.delete(ids);
        return Result.success();
    }
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> get(@PathVariable String id) {
        DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }
    @ApiOperation("更新菜品")
    @PutMapping()
    @CacheEvict(value = "dish", allEntries = true)
    public Result update(@RequestBody DishDTO dish) {
        dishService.update(dish);
        return Result.success();
    }
    @ApiOperation("启停用菜品")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "dish", allEntries = true)
    public Result updateStatus(@PathVariable String status, @RequestParam Integer id) {
        dishService.updateStatus(id, status);
        return Result.success();
    }
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(String categoryId) {
        List<Dish> dish = dishService.listByCategory(Integer.valueOf(categoryId));
        return Result.success(dish);
    }
}
